package cn.jsu.oj.dispatchServer.satellite.handler;

import cn.jsu.oj.dispatchServer.satellite.annotation.HandlerType;
import cn.jsu.oj.dispatchServer.satellite.config.ResourceConfig;
import cn.jsu.oj.dispatchServer.satellite.factory.Factory;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.PackNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.RequestNet;
import cn.jsu.oj.dispatchServer.satellite.pojo.net.ResponseNet;
import cn.jsu.oj.dispatchServer.satellite.util.FileUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;

/**
 * 日志列表处理器
 *
 * @author shan
 * @date 2022/07/11
 */
@Slf4j
@Component("logListHandler")
@HandlerType(typeNo = 12)
public class LogListHandler implements Handler {
    
    @Autowired
    Factory handlerFactory;

    /**
     * 日志目录
     */
    private static final String LOG_PATH = ResourceConfig.getConfigString("client-request-log");
    
    @Override
    public void handle(PackNet<?> packNet) throws FileNotFoundException {
        RequestNet requestNet = (RequestNet) packNet.getMessage();
        String uuid = requestNet.getUuid();
        String[] logNames = FileUtil.dirReader(LOG_PATH);
        ((Handler) handlerFactory.getProduct((short) 4))
                .handle(new PackNet<>(packNet.getChannel()
                        , new ResponseNet(uuid, true, (short) 12, JSON.toJSONString(logNames))));
    }
}
